package com.ycrlkj.android.modulescore.main.activity.app;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.ycrlkj.android.modulescore.main.databinding.ActivityHomeBinding;
import com.ycrlkj.android.modulescore.main.viewmodel.HomeViewModel;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

@Route(path = RoutePath.PAGE_HOME)
public class HomeActivity extends AppCompatActivity {

    private ActivityHomeBinding binding;

    private HomeViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        viewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        binding.setHomeViewModel(viewModel);
        binding.setLifecycleOwner(this);

        setContentView(binding.getRoot());

        ARouter.getInstance().inject(this);

        initTitleBar();
    }


    public void initTitleBar(){
        MTitleBar titleBar = binding.mTitleBar;
        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                finish();
            }

            @Override
            public void setBtnRightOnClickListener() {
                ARouter.getInstance().build(RoutePath.PAGE_VEHICLE_LIST).navigation();
            }
        });
    }


    //应用程序首页需重写返回按钮事件, 阻止按一次返回键就直接退出程序, 需询问用户是否退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            //此处弹窗是由返回按钮触发, .dismissOnBackPressed(false)必须为false, 否则有bug, 弹窗刚出现就被关闭
            XPopupUtil.showModal(this, "确定要退出程序吗?", false, new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    finish();
                }
            });

            return true;
        } else {
            //如果按下的不是返回按钮, 则执行按钮默认操作
            return super.onKeyDown(keyCode, event);
        }
    }

    public void handleToVehicleList(View view) {
        ARouter.getInstance().build(RoutePath.PAGE_VEHICLE_LIST).navigation();
    }

    public void handleToAppointment(View view) {
        ARouter.getInstance().build(RoutePath.PAGE_XX_APPOINTMENT_LIST).navigation();
    }

    public void HandleToFreightOrderTabs(View view) {
        ARouter.getInstance().build(RoutePath.PAGE_FREIGHT_ORDER_TABS).navigation();
    }

    public void HandleToXxOrderList(View view) {
        ARouter.getInstance().build(RoutePath.PAGE_XX_ORDER_LIST).navigation();
    }

    public void HandleToOnlineFreightOrderList(View view) {
        ARouter.getInstance().build(RoutePath.PAGE_ONLINE_FREIGHT_ORDER_LIST).navigation();
    }

    public void HandleToUserCenter(View view) {
        ARouter.getInstance().build(RoutePath.PAGE_USER_CENTER).navigation();
    }

    public void HandleToCompanyList(View view) {
        ARouter.getInstance().build(RoutePath.PAGE_VEHICLE_LIST).navigation();
    }

    public void HandleToAddressList(View view) {
        ARouter.getInstance().build(RoutePath.PAGE_ADDRESS_LIST).navigation();

    }

}